import styles from './login.module.less'
import logo from '../../assets/AAxh.png'
import { Button, Input, Form, Toast } from 'react-vant'
import RotatingAvatar from './rotate/RotatingAvatar'
import axios from '../../api'
import { useNavigate } from 'react-router-dom'

export default function Login() {
    const [form] = Form.useForm()
    const navigate = useNavigate()
    const onFinish = values => {
        console.log(values)
        axios.post("/user/login", values).then(res => {
            console.log(res)
            if (res.data.success) {
                // 登录成功，跳转到首页
                navigate('/home')
            } else {
                Toast.error(res.data.message)
            }
        })
    }
    return (
        <div className={styles.login}>
            <h1 className={styles.title}>记录每一天</h1>
            <div className={styles['login-wrapper']}>
                <div className={styles.avater}>
                    <RotatingAvatar
                        src={logo}
                        alt="Logo"
                        className={styles['avater-img']}
                    />
                </div>
                <Form
                    form={form}
                    onFinish={onFinish}
                    footer={
                        <div style={{ margin: '16px 16px 0' }}>
                            <Button round nativeType='submit' type='primary' block>
                                登录
                            </Button>
                        </div>
                    }
                >
                    <Form.Item
                        rules={[{ required: true, message: '请填写用户名' }]}
                        name='username'
                        label='用户名'
                        labelWidth={50}
                    >
                        <Input placeholder='请输入用户名' />
                    </Form.Item>
                    <Form.Item
                        rules={[{ required: true, message: '请填写密码' }]}
                        name='password'
                        label='密码'
                        labelWidth={50}
                    >
                        <Input placeholder='请输入密码' />
                    </Form.Item>
                </Form>
            </div>
            <p className={styles['login-tips']}>
                没有账号？<a href="/register">点击注册</a>
            </p>
        </div>
    )
}
